package edu.ccut.saturn.component.loader;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import org.dom4j.Document;
import org.dom4j.Element;
import org.osgi.framework.Bundle;

import edu.ccut.saturn.component.IDatabaseConfigInfo;
import edu.ccut.saturn.component.ILogger;
import edu.ccut.saturn.component.SaturnComponentException;
import edu.ccut.saturn.component.SaturnLoggerManager;
import edu.ccut.saturn.component.impl.SaturnConfigInfo;
import edu.ccut.saturn.component.xml.Dom4jUtils;

/**
 * 数据库连接池配置文件的加载类
 * 
 * @author 温建
 * 
 * @version 1.0 2008.9.03
 * 
 * @see IDatabaseConfigInfo
 * 
 */
public class DatabaseConfigLoader {

	private static final String LOGGER_KEY_LOAD_BUNDLE = SaturnLoggerManager.LOGGER_KEY_LOAD_BUNDLE;
	protected static ILogger logger = SaturnLoggerManager.getInstance()
			.getSaturnLogger(LOGGER_KEY_LOAD_BUNDLE);

	private static final String KEY_GROUP = "group";

	private static final String XML_VALIDATION_QUERY = "validationQuery";

	private static final String XML_TEST_ON_BORROW = "testOnBorrow";

	private static final String XML_REMOVE_ABANDONED_TIMEOUT = "removeAbandonedTimeout";

	private static final String XML_REMOVE_ABANDONED = "removeAbandoned";

	private static final String XML_MAX_WAIT = "maxWait";

	private static final String XML_MAX_IDLE = "maxIdle";

	private static final String XML_MAX_ACTIVE = "maxActive";

	private static final String XML_PASSWORD = "password";

	private static final String XML_USERNAME = "username";

	private static final String XML_JDBC_URL = "jdbcurl";

	private static final String XML_JDBC_DRIVER = "jdbcdriver";

	private static final String XML_JNDINAME = "jndiname";

	private static final String XML_CONNECT_TYPE = "connectType";

	private static final String XML_DATABASE_TYPE = "dbType";

	/**
	 * 加载数据库连接池的配置文件信息
	 * 
	 * @param bundle
	 * @param url
	 * @return
	 * @throws SaturnComponentException
	 */
	public static IDatabaseConfigInfo loadDatabaseConfigInfo(Bundle bundle,
			URL url) throws SaturnComponentException {

		InputStream in = null;

		try {
			logger
					.debug("DATABASE CONFIG START : \"" + url
							+ "\"");

			in = url.openStream();
			return loadDatabaseConfigInfo(bundle, in);

		} catch (IOException e) {
			logger.error(
					"Found IOException! load database config file failed in bundle [key="
							+ bundle.getSymbolicName() + "]", e);

			throw new SaturnComponentException(
					"load database config file failed in bundle [key="
							+ bundle.getSymbolicName() + "]", e);

		} catch (Exception e) {
			throw new SaturnComponentException(
					"load database config file failed in bundle [key="
							+ bundle.getSymbolicName() + "]", e);

		} finally {

			try {
				if (in != null) {
					in.close();
				}

			} catch (IOException e) {
				logger.error("database config file input stream close error!",
						e);
			}
			logger.debug("DATABASE CONFIG END");
			logger.debug("");
		}
	}

	public static IDatabaseConfigInfo loadDatabaseConfigInfo(Bundle bundle,
			InputStream in) throws SaturnComponentException {

		IDatabaseConfigInfo dataBaseConfig = new SaturnConfigInfo();
		Document document = Dom4jUtils.getDocment(in);
		Element root = document.getRootElement();
		Element element = root.element(KEY_GROUP);

		if (element != null) {
			dataBaseConfig.setConnectType(element
					.elementText(XML_DATABASE_TYPE));
			
			dataBaseConfig
					.setConnectType(element.elementText(XML_CONNECT_TYPE));
			
			dataBaseConfig.setJndiname(element.elementText(XML_JNDINAME));
			dataBaseConfig.setJdbcdriver(element.elementText(XML_JDBC_DRIVER));
			dataBaseConfig.setJdbcurl(element.elementText(XML_JDBC_URL));
			dataBaseConfig.setUsername(element.elementText(XML_USERNAME));
			dataBaseConfig.setPassword(element.elementText(XML_PASSWORD));

			dataBaseConfig.setMaxActive(element.elementText(XML_MAX_ACTIVE));
			dataBaseConfig.setMaxIdle(element.elementText(XML_MAX_IDLE));
			dataBaseConfig.setMaxWait(element.elementText(XML_MAX_WAIT));
			dataBaseConfig.setRemoveAbandoned(element
					.elementText(XML_REMOVE_ABANDONED));
			
			dataBaseConfig.setRemoveAbandonedTimeout(element
					.elementText(XML_REMOVE_ABANDONED_TIMEOUT));
			
			dataBaseConfig.setTestOnBorrow(element
					.elementText(XML_TEST_ON_BORROW));
			
			dataBaseConfig.setValidationQuery(element
					.elementText(XML_VALIDATION_QUERY));

			return dataBaseConfig;
		}
		return null;
	}
}